var express = require("express")
var router = express.Router()

const tokens = {
  admin: {
    token: "admin-token"
  },
  editor: {
    token: "editor-token"
  }
}

const users = {
  "admin-token": {
    roles: ["admin"],
    introduction: "I am a super administrator",
    avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
    name: "Super Admin"
  },
  "editor-token": {
    roles: ["editor"],
    introduction: "I am an editor",
    avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
    name: "Normal Editor"
  }
}

router.post("/login", (req, res, next) => {
  const { username } = req.body
  const info = tokens[username]

  if (!info) {
    res.statusCode = 50008
    res.error("Login failed, unable to get user details.")
  }

  res.success(info)
})

router.post("/logout", (req, res, next) => {
  res.success(null)
})

router.get("/info", (req, res, next) => {
  const { token } = req.query
  const info = users[token]

  if (!info) {
    res.statusCode = 50008
    res.error("Login failed, unable to get user details.")
  }

  res.success(info)
})

module.exports = router
